<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/13
 * author : weblinuxgame
 * filename: Register.php
 */

namespace App\Repositories;

use App\Core\Utils\Result;
use App\Core\Utils\Message;
use App\Core\Enums\CodeEnum;
use App\Services\UserApiTokenService;
use App\Services\UserService;
use App\Api\Responses\Login\Success;
use App\Core\Interfaces\ResultInterface;

/**
 * 用户注册逻辑
 * Class UserRegister
 * @package App\Repositories
 */
class UserRegisterRepository
{
    /**
     * 注册服务
     * @param array $data
     * @return ResultInterface
     */
    public function register(array $data = []): ResultInterface
    {
        $id = UserService::getInstance()->create($data);
        if (empty($id)) {
            return Result::fail(CodeEnum::UserRegisterFail, Message::message(CodeEnum::UserRegisterFail));
        }
        $user = UserService::getInstance()->getById($id);
        $token = UserApiTokenService::getInstance()->store($user)->toString();
        $info = new Success($user, $token);
        return Result::success($info->data, $info->message, $info->code);
    }
}
